package com.web;

import com.dao.PhoneDao;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/phone/del")
public class PhoneDelServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String id = req.getParameter("id");

        if (id == null) {
            req.setAttribute("message", "id 不能为空！");
            req.getRequestDispatcher("/phone/message.jsp").forward(req, resp);
            return;
        }

        try {
            new PhoneDao().delete(Integer.parseInt(id));
            req.setAttribute("message", "删除成功");
            req.getRequestDispatcher("/phone/message.jsp").forward(req, resp);
        } catch (Exception e) {
            e.printStackTrace();
            req.setAttribute("message", "删除失败！" + e.getLocalizedMessage());
            req.getRequestDispatcher("/phone/message.jsp").forward(req, resp);
        }
    }
}
